package my.main;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import my.bean.Dbcolumn;
import my.util.StringUtil;

public class FileOut {
	/*
	 * 数据库的表名
	 */
	String table = "";
	/*
	 * 代码生成的位置
	 */
	String codePath = "D:/autoCode/oass";

	// 相关类自定义命名,单词以_隔开 例如:想将model命名为ProBase 则定义为pro_base
	String customName = "";

	// 自定义包名,单词以_隔开,不填按照table表名
	String packName = "";
	
	//公司缩写
	String company = "css";

	public FileOut(String table) {
		if (!StringUtil.isEmpty(table)) {
			this.table = table;
		} else {
			throw new RuntimeException("创建FileOut时table不能为空");
		}
		this.customName = table;
	}

	public FileOut(String table, String codePath, String customName, String packName, String company) {
		if (!StringUtil.isEmpty(table)) {
			this.table = table;
		} else {
			throw new RuntimeException("创建FileOut时table不能为空");
		}
		if (!StringUtil.isEmpty(customName)) {
			this.customName = customName;
		} else {
			this.customName = table;
		}
		if (!StringUtil.isEmpty(codePath)) {
			this.codePath = codePath;
		}
		if (!StringUtil.isEmpty(packName)) {
			this.packName = packName;
		} else {
			this.packName = table;
		}
		if(!StringUtil.isEmpty(company)){
			this.company = company;
		}
	}

	// 输出model文件
	public void modelOut(List<Dbcolumn> colList) throws IOException {
		String customName = StringUtil.formatCamel(this.customName);
		File file = new File(codePath + File.separator + "src" + File.separator + "com" + File.separator + company + File.separator
				+ "biz" + File.separator + StringUtil.formatCamel(packName) + File.separator + "model");
		if(!file.exists()){
			file.mkdirs();
		}
		FileWriter writer = new FileWriter(file+ File.separator + StringUtil.upperFrist(customName) +"Model.java");
		StringBuffer sb = new StringBuffer();
		sb.append("package com."+company+".biz." + StringUtil.formatCamel(packName) + ".model;\n\n");
		
		sb.append("import java.util.Date;\n");
//		sb.append("import com.framework.core.common.cmd.BaseQueryCmd;\n\n");
//		sb.append("public class " + StringUtil.upperFrist(customName) + "Model extends BaseQueryCmd {\n\n");
		sb.append("import java.io.Serializable;\n\n");
		sb.append("public class " + StringUtil.upperFrist(customName) + "Model implements Serializable {\n\n");
		
		for(Dbcolumn col : colList){
			sb.append("\tprivate " + col.getColType() + " " + StringUtil.formatCamel(col.getColName()) + ";\n");
		}
		sb.append("\n");
		for(Dbcolumn col : colList){
			sb.append("\tpublic " + col.getColType() + " get" + StringUtil.upperCamelFrist(col.getColName()) + "() {\n");
				sb.append("\t\treturn " + StringUtil.formatCamel(col.getColName()) +";\n");
			sb.append("\t}\n\n");
			
			sb.append("\tpublic void set" + StringUtil.upperCamelFrist(col.getColName()) + "("+col.getColType()+" "+StringUtil.formatCamel(col.getColName())+") {\n");
				sb.append("\t\tthis." + StringUtil.formatCamel(col.getColName()) +" = "+StringUtil.formatCamel(col.getColName())+";\n");
			sb.append("\t}\n\n");
		}
		sb.append("}");
		writer.write(sb.toString());
		writer.flush();
		writer.close();
	}

	// 输出mybatis文件
	public void mybatisOut(List<Dbcolumn> colList) throws IOException{
		File file = new File(codePath + File.separator + "src" + File.separator + "com" + File.separator + company + File.separator
				+ "biz" + File.separator + StringUtil.formatCamel(packName) + File.separator + "dao");
		if(!file.exists()){
			file.mkdirs();
		}
		FileWriter writer = new FileWriter(file+ File.separator + StringUtil.toMinus(customName) +"-mapper.xml");
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
		sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\n");
		sb.append("<mapper namespace=\"com."+company+".biz."+StringUtil.formatCamel(packName)+".dao.I"+StringUtil.formatCamel(this.customName)+"Dao\" >\n");
			//insert
			String modelName = "com." + company + ".biz." + StringUtil.formatCamel(packName) + ".model."+StringUtil.upperCamelFrist(customName) + "Model";
			sb.append("\t<insert id=\"save\" parameterType=\""+modelName+"\">\n");
				sb.append("\tinsert into " + table + "(\n");
				for(int i=0;i<colList.size();i++){
					sb.append("\t\t"+colList.get(i).getColName());
					if(i<colList.size()-1){
						sb.append(",");
					}
					sb.append("\n");
				}
				sb.append("\t) values (\n");
				for(int i=0;i<colList.size();i++){
					sb.append("\t\t#{"+StringUtil.formatCamel(colList.get(i).getColName())+",jdbcType=VARCHAR}");
					if(i<colList.size()-1){
						sb.append(",");
					}
					sb.append("\n");
				}
				sb.append("\t)\n");
			sb.append("\t</insert>\n");
			//update
			sb.append("\n");
			sb.append("\t<update id=\"update\" parameterType=\""+modelName+"\">\n");
				sb.append("\tupdate "+ table + "\n");
				sb.append("\t<set>\n");
				for(Dbcolumn col : colList){
					sb.append("\t\t"+col.getColName() + " = #{"+StringUtil.formatCamel(col.getColName())+",jdbcType=VARCHAR},\n");
				}
				sb.append("\t</set>\n");
				sb.append("\t<where>\n");
					sb.append("\t\t"+colList.get(0).getPriKey() + " = " + "#{"+StringUtil.formatCamel(colList.get(0).getPriKey())+"}\n");
				sb.append("\t</where>\n");
			sb.append("\t</update>\n");
			//selectById
			sb.append("\n");
			sb.append("\t<select id=\"selectById\" parameterType=\""+modelName+"\" resultMap=\""+StringUtil.formatCamel(customName)+"\">\n");
			sb.append("\tselect\n");
			for(int i=0;i<colList.size();i++){
				sb.append("\t\t"+colList.get(i).getColName());
				if(i<colList.size()-1){
					sb.append(",");
				}
				sb.append("\n");
			}
			sb.append("\tfrom\n");
			sb.append("\t\t"+table+"\n");
			sb.append("\t<where>\n");
			sb.append("\t\t"+colList.get(0).getPriKey() + " = " + "#{"+StringUtil.formatCamel(colList.get(0).getPriKey())+"}\n");
			sb.append("\t</where>\n");
			sb.append("\t</select>\n");
			//selectAll
			sb.append("\n");
			sb.append("\t<select id=\"selectAll\" parameterType=\""+modelName+"\" resultMap=\""+StringUtil.formatCamel(customName)+"\">\n");
			sb.append("\tselect\n");
			for(int i=0;i<colList.size();i++){
				sb.append("\t\t"+colList.get(i).getColName());
				if(i<colList.size()-1){
					sb.append(",");
				}
				sb.append("\n");
			}
			sb.append("\tfrom\n");
			sb.append("\t\t"+table+"\n");
			sb.append("\t</select>\n");
			//delete
			sb.append("\n");
			sb.append("\t<delete id=\"deleteById\" parameterType=\""+modelName+"\">\n");
			sb.append("\t\tdelete from " + table + " where " + colList.get(0).getPriKey() + " = #{"+StringUtil.formatCamel(colList.get(0).getPriKey())+"}\n");
			sb.append("\t</delete>\n");
			//resultMap
			sb.append("\n");
			sb.append("\t<resultMap type=\""+modelName+"\" id=\""+StringUtil.formatCamel(customName)+"\">\n");
			for(Dbcolumn col : colList){
				//jdbcType待完善
				sb.append("\t\t<result column=\""+col.getColName()+"\" jdbcType=\"VARCHAR\" property=\""+StringUtil.formatCamel(col.getColName())+"\"/>\n");
			}
			sb.append("\t</resultMap>\n");
			sb.append("\n");
		sb.append("</mapper>");
		writer.write(sb.toString());
		writer.flush();
		writer.close();
	}
	
	// 输出dao接口文件
	public void daoOut(List<Dbcolumn> colList) throws IOException{
		File file = new File(codePath + File.separator + "src" + File.separator + "com" + File.separator + company + File.separator
				+ "biz" + File.separator + StringUtil.formatCamel(packName) + File.separator + "dao");
		if(!file.exists()){
			file.mkdirs();
		}
		FileWriter writer = new FileWriter(file+ File.separator + "I" + StringUtil.formatCamel(customName) +"Dao.java");
		StringBuffer sb = new StringBuffer();
		sb.append("package com." + company + ".biz." + StringUtil.formatCamel(packName) + ".dao;\n");
		sb.append("\n");
		sb.append("import java.util.List;\n");
		sb.append("import " + "com." + company + ".biz." + StringUtil.formatCamel(packName) + ".model."+StringUtil.upperCamelFrist(customName) + "Model;\n");
		sb.append("\n");
		sb.append("public interface " + "I" + StringUtil.formatCamel(customName) +"Dao" + " {\n");
		//insert	save
		sb.append("\tpublic void save ("+StringUtil.upperCamelFrist(customName)+"Model " + StringUtil.formatCamel(customName) + ");\n");
		//update	update
		sb.append("\tpublic void update ("+StringUtil.upperCamelFrist(customName)+"Model " + StringUtil.formatCamel(customName) + ");\n");
		//selectById	selectById
		sb.append("\tpublic " + StringUtil.upperCamelFrist(customName)+"Model " + "selectById(String id);\n");
		//selectAll	selectAll
		sb.append("\tpublic List<" + StringUtil.upperCamelFrist(customName)+"Model> " + "selectAll();\n");
		//delete	deleteById
		sb.append("\tpublic void deleteById (String id);\n");
		sb.append("}");
		writer.write(sb.toString());
		writer.flush();
		writer.close();
	}
	// 输出server接口文件

	// 输出server实现接口文件

}
